<?php
require_once APPPATH . "core/db.php";

class Crash_model {
    private $main_table;
    private $db;

    function __construct() {
        $this->db = DB::getInstance();
        $this->main_table = "crash_reports";
    }

    public function search($key, $value) {
        $this->db->where($key, $backtrace);
        $this->db->get($this->main_table);
        $result = $this->db->fetch_all();
        return $result;
    }
    public function search_like($key, $backtrace) {
        $this->db->where_like($key, $backtrace);
        $this->db->get($this->main_table);
        $result = $this->db->fetch_all();
        return $result;
    }
    public function get_all() {
        $this->db->get($this->main_table);
        $result = $this->db->fetch_all();
        return $result;
    }
    public function get_by_id($id) {
        $this->db->where('id', $id);
        $this->db->get($this->main_table);
        $row = $this->db->fetch_row();
        return $row;
    }
    function insert_crash_report($crash) {
        $insert_array = array();
        if ($crash instanceof crash_report) {
            $array_crash = get_object_vars($crash);
            $insert_array = array();
            foreach ($array_crash as $key => $value) {
                if (!empty($value)) {
                    $insert_array[$key] = $value;
                }
            }
        } else {
            $insert_array = $crash;
        }
        $this->db->insert($this->main_table, $insert_array);
    }
    public function get_similar_crash($crash, $meta_percent = 85) {
        if (!($crash instanceof crash_report)) {
            throw new Exception("only crash report object need!");
        }
        $this->db->select(array('id', 'backtrace'));
        $this->db->where_like('variant', $crash->get_project());
        $this->db->where('process_type', $crash->process_type);
        $this->db->get($this->main_table);
        $crash_reports = $this->db->fetch_all();
        $ret = 0;
        if (count($crash_reports) > 0) {
            foreach($crash_reports as $row) {
                similar_text($crash->backtrace, $row['backtrace'], $percent);
                if ($percent > $meta_percent) {
                    $ret = $row['id'];
                    break;
                }
            }
        }
        return $ret;
    }
    public function increase($where_array=array(), $key_array) {
        foreach ($where_array as $key => $value) {
            $this->db->where($key, $value);
        }
        return $this->db->increase($this->main_table, $key_array);
    }
    public function update($where_array=array(), $set_array) {
        foreach ($where_array as $key => $value) {
            $this->db->where($key, $value);
        }
        return $this->db->update($this->main_table, $set_array);
    }
    public function remove($where_array=array()) {
        foreach ($where_array as $key => $value) {
            $this->db->where($key, $value);
        }
        return $this->db->delete($this->main_table);
    }
}
